package com.ciaojian.core.bytedance.auth;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ciaojian.core.bytedance.MiniAppRequestApi;
import com.ciaojian.core.bytedance.ByteDanceConfig;
import com.ciaojian.core.bytedance.ByteDanceMiniConfig;
import com.ciaojian.core.bytedance.api.ByteDanceRequestApi;
import com.ciaojian.core.bytedance.vo.ByteDanceClientTokenVo;
import com.ciaojian.core.bytedance.vo.ByteMiniAccessToken;
import com.ciaojian.core.util.JsonUtil;
import com.ciaojian.core.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class ByteDanceTokenProvider {

    //token (无需用户授权
    public String getBytedanceClienttoken(String bytedance_clienttoken) {
        String access_token = RedisUtil.getKey(bytedance_clienttoken);
        if (ObjectUtil.isNull(access_token)) {
            try {
                Map<String, Object> param = new HashMap<String, Object>();
                //可以设置多个input的name，value
                param.put("grant_type", "client_credential");
                if ("BYTEDANCE_CLIENTTOKEN_MINI".equals(bytedance_clienttoken)) {
                    param.put("client_secret", ByteDanceMiniConfig.SECRET_ID);
                    param.put("client_key", ByteDanceMiniConfig.APP_ID);
                }
                if ("BYTEDANCE_CLIENTTOKEN".equals(bytedance_clienttoken)) {
                    param.put("client_secret", ByteDanceConfig.CLIENT_SECRET);
                    param.put("client_key", ByteDanceConfig.CLIENT_KEY);
                }
                if ("BYTEDANCE_MINIAPP_ACCESSTOKEN".equals(bytedance_clienttoken)) {
                    param.put("appid", ByteDanceMiniConfig.APP_ID);
                    param.put("secret", ByteDanceMiniConfig.SECRET_ID);
                    param.put("grant_type", "client_credential");
                    //定义接收数据
                    JSONObject result = new JSONObject();

                    String access_token_v2 = ByteDanceRequestApi.sendRequest(MiniAppRequestApi.ACCESS_TOKEN_V2, param);

                    result = JSON.parseObject(access_token_v2);
                    ByteMiniAccessToken byteMiniAccessToken = result.toJavaObject(ByteMiniAccessToken.class);
                    RedisUtil.setKeyTime(bytedance_clienttoken, 300, byteMiniAccessToken.getData().getAccess_token(), DateUnit.SECOND);
                    return byteMiniAccessToken.getData().getAccess_token();
                }


                //定义接收数据
                JSONObject result = new JSONObject();
                String clientToken = HttpRequest.post(ByteDanceConfig.CLIENT_TOKEN_URL).contentType("multipart/form-data").form(param).execute().body();

                result = JSON.parseObject(clientToken);

                ByteDanceClientTokenVo byteDanceClientTokenVo = JsonUtil.fromJson(clientToken, ByteDanceClientTokenVo.class);
                if (ObjectUtil.isNotNull(byteDanceClientTokenVo.getData().getAccess_token())) {
                    RedisUtil.setKeyTime(bytedance_clienttoken, 300, byteDanceClientTokenVo.getData().getAccess_token(), DateUnit.SECOND);
                    access_token = byteDanceClientTokenVo.getData().getAccess_token();
                }
                return access_token;
            } catch (Exception e) {

            }
        }
        return access_token;
    }
}
